import script, player, pygame
from pygame.locals import *	

class Menu:
	def __init__(self,idn, moff, type):
		self.type = type
		self.menu_items = []
		self.sub_menu = []
		self.ply = 0
		self.id = id
		self.clicked = 0
		fname = "resources/menu/menu" + idn +".txt"
		menu_item_script = script.Script(fname)
		self.menunum = menu_item_script.get_nvalue("MENUNUM","int","=")
		print menu_item_script.get_nvalue("TITLE","string","=")
		self.main_image = pygame.image.load( menu_item_script.get_nvalue("TITLE","string","="))
		self.main_image.set_colorkey((255,0,255))
		self.menu_surface = pygame.Surface((800,600))
		for i in range(0,self.menunum):
			menustr = "S" + str(i)
			menu_item_tmp = Menu_Item(menu_item_script.get_nvalue(menustr,"string","="))
			self.menu_items.append(menu_item_tmp)
		print len(self.menu_items)
		self.mo = moff
		self.num_fnt = pygame.font.Font("resources/menu/xfiles.ttf",32)
		self.total_points = 0
		self.points_txt = pygame.image.load("resources/graphics/hud/points.bmp")
		self.hook_icon = pygame.image.load("resources/graphics/hook_icon.bmp")
		self.shoe_icon = pygame.image.load("resources/graphics/shoe_icon.bmp")
		self.shovel_icon = pygame.image.load("resources/graphics/shovel_icon.bmp")
		self.hook_icon.set_colorkey((255,0,255))
		self.points_txt.set_colorkey((255,0,255))
		self.shoe_icon.set_colorkey((255,0,255))
		self.shovel_icon.set_colorkey((255,0,255))
	
	def do_action(self,g):
		offset = 100
		if self.type == "traditional":
			if g.poll == 1:
				self.clicked = 1
			if self.clicked == 1:
				for i in range(0, self.menunum):
					if self.menu_items[i].on_mouse(0,i*self.mo+300,self.mo) == True:
						return self.menu_items[i].text
			g.poll = 0
			self.clicked = 0
		elif self.type == "selection":
			self.total_points = g.global_points
			if g.poll == 1:
				self.clicked = 1
				if self.clicked == 1:
					#B1 = pygame.Rect((pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1]),(32,32))
					for i in range(0, self.menunum):
						B1 = pygame.Rect((pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1]),(32,32))
						B2 = pygame.Rect(((i%4)*100,offset),(80,80))
						if (i+1) % 4 == 0:
							offset += 150
						if B1.colliderect(B2):
							return i
								
		
	def draw(self,level_status):
		offset = 100
		if self.type == "traditional":
			self.menu_surface.blit(self.main_image,(200,0))
			for i in range(0,self.menunum):
				if (self.menu_items[i].on_mouse(0,i*self.mo+300,self.mo) == True):
					self.menu_surface.blit(self.menu_items[i].draw((0,0,255)), (0,i*self.mo+300))
				else:
					self.menu_surface.blit(self.menu_items[i].draw((255,0,0)), (0,i*self.mo+300))
		elif self.type == "selection":
			self.menu_surface.blit(self.main_image,(0,0))
			for i in range(0,self.menunum):
				B1 = pygame.Rect(((i%4)*100,offset),(80,80))
				border = pygame.Rect(((i%4)*100,offset),(80,80))
				if level_status[i] == 0:
					pygame.draw.rect(self.menu_surface,(255,0,0),B1)
				elif level_status[i] == 1:
					pygame.draw.rect(self.menu_surface,(0,255,0),B1)
				elif level_status[i] == 2:
					pygame.draw.rect(self.menu_surface,(0,0,255),B1)
				pygame.draw.rect(self.menu_surface,(255,255,255),border,1)
				num_txt = pygame.Surface((60,60))
				num_txt.fill((255,0,255))
				num_txt = self.num_fnt.render(str(i),True,(255,255,255))
				self.menu_surface.blit(num_txt,(((i%4)*100),offset))
				if (i+1) % 4 == 0:
					offset += 150
			self.menu_surface.blit(self.hook_icon,(400,100))
			self.menu_surface.blit(self.shoe_icon,(400,250))
			self.menu_surface.blit(self.shovel_icon,(400,400))
			self.menu_surface.blit(self.points_txt,(250,0))
			pnt_txt = self.num_fnt.render(str(self.total_points),True,(255,0,0))
			self.menu_surface.blit(pnt_txt,(350,0))
		return self.menu_surface
	
	def del_surfaces(self):
		self.menu_surface.fill((0,0,0))

			
class Menu_Item:
	def __init__(self,txt):
		self.text = txt
		self.fnt = pygame.font.Font("resources/menu/xfiles.ttf",32)
		
	def on_mouse(self,x,y,offset):
		Px = pygame.mouse.get_pos()[0]
		Py = pygame.mouse.get_pos()[1]
		if( Px > x and Px < x + 800 ):
			if( Py > y and Py < y + offset):
				return True
		return False
		
	def draw(self,col):
		self.menu_item_surface = self.fnt.render(self.text,True,col)
		return self.menu_item_surface
